// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.commands;

import java.util.List;

import org.dengues.core.process.EConnectionType;
import org.dengues.core.process.ICompConnection;
import org.dengues.designer.ui.editors.AbstractGEFCommand;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.designer.ui.process.models.CompConnection;
import org.dengues.designer.ui.process.models.CompNode;
import org.dengues.designer.ui.process.models.CompProcess;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-10-9 下午11:19:24
 */
public class CompConnectionCreateCommand extends AbstractGEFCommand {

    private CompConnection connection;

    private CompNode source;

    private CompNode target;

    /**
     * Qiang.Zhang.Adolf@gmail.com CompConnectionCreateCommand constructor comment.
     * 
     * @param node
     */
    public CompConnectionCreateCommand(CompNode source) {
        setLabel(Messages.getString("CompConnectionCreateCommand.create")); //$NON-NLS-1$
        this.source = source;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#canExecute()
     */
    @Override
    public boolean canExecute() {
        if (target != null) {
            if (source.equals(target)) {
                return false;
            }
            List<? extends ICompConnection> outs = source.getOutgoingConnections();
            for (ICompConnection conn : outs) {
                if (conn.getTarget().equals(target)) {
                    return false;
                }
            }
            List<? extends ICompConnection> ins = target.getIncomingConnections();
            for (ICompConnection conn : ins) {
                if (conn.getSource().equals(source)) {
                    return false;
                }
            }
            return target.isInput(EConnectionType.MAIN.toString());
        }
        return source.isOutput(EConnectionType.MAIN.toString());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#undo()
     */
    @Override
    public void undo() {
        connection.disconnect();
        ((CompProcess) connection.getCompProcess()).removeCompConnectionLabel(connection.getConnectionLabel());
        ((CompProcess) connection.getCompProcess()).removeCompConnectionTrac(connection.getConnectionTrac());

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#execute()
     */
    @Override
    public void execute() {
        connection = new CompConnection(source, target, EConnectionType.MAIN);
        ((CompProcess) connection.getCompProcess()).addCompConnectionLabel(connection.getConnectionLabel());
        ((CompProcess) connection.getCompProcess()).addCompConnectionTrac(connection.getConnectionTrac());
    }

    /**
     * Getter for source.
     * 
     * @return the source
     */
    public CompNode getSource() {
        return this.source;
    }

    /**
     * Sets the source.
     * 
     * @param source the source to set
     */
    public void setSource(CompNode source) {
        this.source = source;
    }

    /**
     * Getter for target.
     * 
     * @return the target
     */
    public CompNode getTarget() {
        return this.target;
    }

    /**
     * Sets the target.
     * 
     * @param target the target to set
     */
    public void setTarget(CompNode target) {
        this.target = target;
    }

}
